package cn.com.cetccst.datacenter.collection.entity;

import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey;
import androidx.room.TypeConverter;
import androidx.room.TypeConverters;

import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.List;

import cn.com.cetccst.datacenter.DataUtils;

@Entity(tableName = LocationRecordEntity.TABLE_NAME)
@TypeConverters(LocationRecordEntity.CodeListConverter.class)
public class LocationRecordEntity {
    public static final String TABLE_NAME = "location_record_entity";

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id")
    private long id;

    @ColumnInfo(name = "latitude")
    private double latitude;
    @ColumnInfo(name = "longitude")
    private double longitude;
    @ColumnInfo(name = "success")
    private boolean success;
    @ColumnInfo(name = "accurate")
    private boolean accurate;
    @ColumnInfo(name = "locate_time")
    private long locateTime;
    @ColumnInfo(name = "error_codes")
    private List<Integer> errorCodes;
    @ColumnInfo(name = "address")
    private String address;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public double getLatitude() {
        return latitude;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public double getLongitude() {
        return longitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public boolean isAccurate() {
        return accurate;
    }

    public void setAccurate(boolean accurate) {
        this.accurate = accurate;
    }

    public long getLocateTime() {
        return locateTime;
    }

    public void setLocateTime(long locateTime) {
        this.locateTime = locateTime;
    }

    public List<Integer> getErrorCodes() {
        return errorCodes;
    }

    public void setErrorCodes(List<Integer> errorCodes) {
        this.errorCodes = errorCodes;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public static class CodeListConverter {
        @TypeConverter
        public List<Integer> str2intList(String value) {
            Type typeToken = new TypeToken<List<Integer>>() {
            }.getType();
            return DataUtils.getGson().fromJson(value, typeToken);
        }

        @TypeConverter
        public String intList2str(List<Integer> value) {
            return DataUtils.getGson().toJson(value);
        }
    }

    public JSONObject toJson() {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("longitude", String.valueOf(longitude));
            jsonObject.put("latitude", String.valueOf(latitude));
            jsonObject.put("address", address);
            jsonObject.put("locateTime", locateTime);
            jsonObject.put("isSuccess", success);
            jsonObject.put("accurate", accurate);

            JSONArray array = new JSONArray(errorCodes);
            jsonObject.put("errCodes", array);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonObject;
    }

}
